<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);
if(isset($_REQUEST['search'])){
	$persons = $MAIN->persons->searchPersons($_REQUEST['search']);
	foreach($persons as $person){
	?>
	<div onclick="fillFormPerson(<?php echo $_REQUEST['i'];?>,<?php echo($person['id']);?>);" id="person_<?php echo($person['id']);?>"><?php echo($person['firstname']." ".$person['lastname']);?></div>
	<?php
	}
	?>
	<script type="text/javascript">
	var persons={};
	<?php	
	foreach($persons as $person){
	?>
	persons[<?php echo($person['id']);?>]=<?php echo(json_encode($person));?>;	 
	<?php
	}
	?>
	</script>
	<?php
	exit();
}
if(isset($_REQUEST['searchleader'])){
	$s=mysql_real_escape_string($_REQUEST['searchleader']);
	$sql = "select `".tblPrefix."persons`.*,`".tblPrefix."users`.*,`".tblPrefix."users`.`active`,`".tblPrefix."persons`.`id`,`".tblPrefix."persons`.`association_id`, 
			(SELECT MAX(`".tblPrefix."logs`.`modif`) FROM `".tblPrefix."logs`								 
				WHERE `".tblPrefix."users`.`id` = `".tblPrefix."logs`.`user_id` 
				AND `".tblPrefix."logs`.`type` = 'LOGIN' 
				AND `".tblPrefix."logs`.`description` = 'success') as last_login 
		from `".tblPrefix."persons`			
		INNER JOIN `".tblPrefix."users` ON (`".tblPrefix."persons`.`user_id`=`".tblPrefix."users`.`id` AND `".tblPrefix."users`.`grouplevel`=70)
		WHERE (`".tblPrefix."persons`.`firstname` LIKE '%".$s."%' OR `".tblPrefix."persons`.`lastname` LIKE '%".$s."%')
		order by `firstname` ASC";
	$persons=$MAIN->returnArray($sql);	
	foreach($persons as $person){
	?>
	<div onclick="fillFormLeader(<?php echo($person['id']);?>);" id="person_<?php echo($person['id']);?>"><?php echo($person['firstname']." ".$person['lastname']);?></div>
	<?php
	}
	?>
	<script type="text/javascript">
	var leaders={};
	<?php	
	foreach($persons as $person){
	?>
	leaders[<?php echo($person['id']);?>]=<?php echo(json_encode($person));?>;	 
	<?php
	}
	?>
	</script>
	<?php
	exit();
}
if(isset($_POST['txt_person_firstname'])){
	if($personId=$MAIN->persons->setPerson($_POST['txt_person_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=persons&id=<?php echo($personId);?>&view=edit';}).delay(3000);</script>
	<?php
	}
	exit();
}
if(isset($_POST['txt_person_id']) && isset($_POST['txt_user_id'])){
	$personId=$_POST['txt_person_id'];
	if($userId = $MAIN->users->setUser($_POST['txt_user_id'],$_POST)){
		$MAIN->persons->setPersonUser($personId,$userId);
		?>
		<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=persons&id=<?php echo($personId);?>&view=edit';}).delay(3000);</script>
		<?php
	}
	exit();
}
if(isset($_REQUEST['del'])){
	$sql="SELECT * FROM `ibn_registrations` WHERE ".$_REQUEST['del']." in (`members`)";
	$res=$MAIN->returnArray($sql);
	if(count($res)>0){
	?>
	<script type="text/javascript">
		alert('Fel ! \nDenna person kan inte raderas eftersom den redan används i systemet!');
	</script>
	<?php	
	}else{
		if($MAIN->persons->delPerson($_REQUEST['del'])){
		?>
		<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=persons&view=all';}).delay(3000);</script>
		<?php	
		};
	}
	exit();
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
	$ord="ASC";
	$orderBy="lastname";
	if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
	$nextOrd="DESC";
	if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
	$persons = $MAIN->persons->getPersons($orderBy,$ord);
?>
	<h1>Alla medlemar/personer</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>		
		<th><a href="?cat=persons&amp;view=all&amp;ordby=firstname&amp;ord=<?php echo($nextOrd);?>">Förnamn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=persons&amp;view=all&amp;ordby=lastname&amp;ord=<?php echo($nextOrd);?>">Efternamn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=persons&amp;view=all&amp;ordby=email&amp;ord=<?php echo($nextOrd);?>">E-postadress</a></th>
		<th style="width:10px;"></th>
		<th><a href="#">Förening</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=persons&amp;view=all&amp;ordby=pers&amp;ord=<?php echo($nextOrd);?>">Person nr.</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=persons&amp;view=all&amp;ordby=tel&amp;ord=<?php echo($nextOrd);?>">Tel</a></th>
		<th style="width:10px;"></th>
		<th>Administrera</th>		
		<th style="width:10px;"></th>
		<th>Notis</th>		
		<th style="width:10px;"></th>
		<th><a href="?cat=persons&amp;view=all&amp;ordby=last_login&amp;ord=<?php echo($nextOrd);?>">Sista inloggning</a></th>
	</tr>
	<?php
	foreach($persons as $person){
		if($person['user_id']!=$MAIN->currentUser['id']){
	?>
	<tr class="entry">		
		<td><?php echo($person['firstname']);?></td>
		<td></td>
		<td><?php echo($person['lastname']);?></td>
		<td></td>
		<td><?php echo (isset($person['email']) && $person['email']!='')?$person['email']:$person['emailp'];?></td>
		<td></td>
		<td><?php if(isset($person['association_id']) && $person['association_id']!=''){
			$ass=$MAIN->associations->getAssociation($person['association_id']);
			if($MAIN->currentUser['grouplevel']<=50){
				echo('<a href="?cat=associations&view=edit&id='.$ass['id'].'">'.$ass['name'].'</a>');
			}else{
				echo($ass['name']);
			} 			
		}else{echo('');};?></td>
		<td></td>
		<td><?php echo($person['pers']);?></td>
		<td></td>
		<td><?php echo($person['tel']);?></td>
		<td></td>
		<td class="tools">
			<?php
			if($MAIN->currentUser['grouplevel']<=50){
			?>
			<a href="javascript:void(0);" onclick="delUser(<?php echo($person['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>
			<?php
			}
			if($person['user_id']!=0 && $person['user_id']!=$MAIN->currentUser['id']){
			?>
				<?php if($person['active']==0){?>
				<a href="javascript:void(0);" onclick="toggleActivate(<?php echo($person['user_id']);?>);"><img src="images/btn_not_active.png" alt="Aktivera" title="Aktivera"/></a>
				<?php }else{?>
				<a href="javascript:void(0);" onclick="toggleActivate(<?php echo($person['user_id']);?>);"><img src="images/btn_active.png" alt="Inaktivera" title="Inaktivera"/></a>
				<?php }?>
			<?php
			}
			?>
			<a href="?cat=persons&amp;view=edit&amp;id=<?php echo($person['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera" title="Redigera"/></a>
		</td>
		<td></td>
		<td>
			<?php 
			if($person['association_id']==0){
			?>
			<i style="color:#F96868; font-size:8px;">Obs : Utan Förening !</i>
			<?php
			};
			?>
		</td>
		<td></td>
		<td>&nbsp;&nbsp;<?php if($person['last_login']!=""){echo(date('Y-m-d H:s',strtotime($person['last_login'])));}?></td>
	</tr>
	<?php
		}
	}
	?>
	</table>
	<script type="text/javascript">
		var delUser = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('persons.php','del='+id);
			}
		}
		var toggleActivate = function(uid){
			var msg='Bekräfta aktivering/inaktivering!';
			if(confirm(msg)){
				ajaxGet('persons.php','act='+uid);
			}
		}
	</script>
<?php
	break;
	case 'add':
	case 'edit':
		$person = $MAIN->persons->getPerson($_REQUEST['id']);		
?>
	<!--
	<hr/>-->
	</hr/>
	<table cellpadding="0" cellspacing="0" style="width:100%;">
		<tr>
			<td>
				<h1>Användaruppgifter </h1>
				<form name="frm_profile" id="frm_profile" action="persons.php" method="post" onsubmit="return false;">
					<input type="hidden" name="txt_person_id" value="<?php echo($person['id']);?>"/>
					<?php
					if(@$person['user_id']==""){$person['user_id']=0;}
					?>
					<input type="hidden" name="txt_user_id" value="<?php echo($person['user_id']);?>"/>
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td>Förnamn</td>
						<td><input class="required" type="text" name="txt_person_firstname" value="<?php echo(@$person['firstname']);?>"/></td>
					</tr>
					<tr>
						<td>Efternamn</td>
						<td><input class="required" type="text" name="txt_person_lastname" value="<?php echo(@$person['lastname']);?>"/></td>
					</tr>
					<tr>
						<td>Personnummer</td>
						<td><input class="required validate-pers" type="text" name="txt_person_pers" value="<?php echo(@$person['pers']);?>"/></td>
					</tr>
					<tr>
						<td>Adress</td>
						<td><input class="required" type="text" name="txt_person_address" value="<?php echo(@$person['address']);?>"/></td>
					</tr>
					<tr>
						<td>Postnummer</td>
						<td><input class="required" type="text" name="txt_person_zip" value="<?php echo(@$person['zip']);?>"/></td>
					</tr>
					<tr>
						<td>Ort</td>
						<td><input class="required" type="text" name="txt_person_city" value="<?php echo(@$person['city']);?>"/></td>
					</tr>
					<tr>
						<td>Telefonnummer</td>
						<td><input class="required" type="text" name="txt_person_tel" value="<?php echo(@$person['tel']);?>"/></td>
					</tr>
					<tr>
						<td>Förening</td>
						<td>
							<?php
							$associations = $MAIN->associations->getAssociations();
							?>
							<select name="ddl_association" class="validate-custom-required emptyValue:'--'">
								<?php
								if($MAIN->currentUser['grouplevel']>=70){
									foreach($associations as $as){
										if($as['id']==$MAIN->currentUser['association_id']){
								?>
								<option selected="selected" value="<?php echo($as['id']);?>"><?php echo($as['name']);?></option>
								<?php		
										}
									}
								}else{
								?>
								<option value="--">-- ange förening --</option>
								<?php
								foreach($associations as $as){
								?>
								<option <?php if($person['association_id']==$as['id']){?>selected="selected"<?php }?> value="<?php echo($as['id']);?>"><?php echo($as['name']);?></option>
								<?php
								}
								?>
								<?php
								}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td colspan="2" style="text-align:right;">
							<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_profile');"/>
						</td>
					</tr>
				</table>
				</form>
			</td>
			<td style="vertical-align: top;">
				<?php				
				if($person['user_id']!=0){				
					if($user = @$MAIN->users->getUser($person['user_id'])){
					?>
					<h1>Inloggningsuppgifter</h1>
					<form name="frm_user" id="frm_user" action="users.php" method="post" onsubmit="return false;">
					<input type="hidden" name="txt_user_id" value="<?php echo($user['id']);?>"/>
					<table cellpadding="0" cellspacing="0">
						<tr>
							<td>E-post address</td>
							<td><input autocomplete="off" type="text" readonly="readonly" name="txt_email" value="<?php echo(@$user['email']);?>"/></td>
						</tr>		
						<tr>
							<td>Lösenord</td>
							<td><input readonly="readonly" autocomplete="off" type="password" name="txt_pass" value="<?php if(@$user['drowp']!=""){echo($MAIN->decode(@$user['drowp']));}?>"/></td>
						</tr>
						<tr>
							<td>Grupp</td>
							<td>
								<?php
								$groups = $MAIN->groups->getGroups();
								?>
								<select name="ddl_rights" readonly="readonly" disabled="diasbled">
								<?php
								foreach($groups as $g){
								?>
									<option value="<?php echo($g['rights']);?>" <?php if($user['grouplevel']==$g['rights']){?>selected="selected"<?php }?>><?php echo($g['display_name']);?></option>
								<?php
								}
								?>
								</select>
							</td>
						</tr>		
					</table>
					</form>
					<?php
					}
				}else if($person['id']!=0){
					if($MAIN->currentUser['grouplevel']<50){
				?>
				<h1>Koppla användare/skapa konto</h1>
				<?php
				$users = $MAIN->users->getUsers();				
				?>
				<select name="ddl_user" onchange="if(this.value!=null){ajaxGet('users.php','getUser&id='+this.value+'&pid=<?php echo($person['id']);?>',$('set_user'));}">
					<option value="null">-- ange användare --</option>
				<?php				
				foreach($users as $u){
					if($u['person_id']==0){
				?>
					<option value="<?php echo($u['id']);?>"><?php echo($u['email']);?></option>
				<?php
					}
				}
				?>
					<option value="0">skapa konto</option>
				</select>
				<div id="set_user">
					
				</div>				
				<?php	
					}
				}
				?>
			</td>
		</tr>
	</table>
	
	<hr/>
	<?php
	if($_REQUEST['view']!="add"){
	?>
	<h2>Loggar</h2>
	<?php
	$_REQUEST['user_id']=$person['id'];
	$_REQUEST['log_type']="user";
	include_once("logs.php");
	?>
<?php
	}
	break;
}
?>
</div>